package com.my.mvvmrapid.ui.main.fragment.homepersonal;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemChildClickListener;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.google.android.material.appbar.AppBarLayout;
import com.my.mvvmrapid.BR;
import com.my.mvvmrapid.R;
import com.my.mvvmrapid.databinding.FragmentHomePersonalBinding;
import com.my.mvvmrapid.databinding.FragmentHomeShopTestBinding;
import com.my.mvvmrapid.ui.fuction.aissocket.AisSocketActivity;
import com.my.mvvmrapid.ui.fuction.dialog.DialogActivity;
import com.my.mvvmrapid.ui.fuction.from.FromActivity;
import com.my.mvvmrapid.ui.fuction.selector.SelectorActivity;
import com.my.mvvmrapid.ui.main.fragment.homepersonal.adapter.FunctionAdapter;
import com.my.mvvmrapid.ui.main.fragment.homeshop.shoptest.adapter.ListAdapter;
import com.trello.rxlifecycle2.components.support.RxAppCompatActivity;
import com.zhongjh.mvvmrapid.base.ui.BaseFragment;
import com.zhongjh.mvvmrapid.utils.DisplayMetricsUtil;
import com.zhongjh.mvvmrapid.view.SpaceItemGridDecoration;

import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.List;

/**
 * 个人中心
 * @author zhongjh
 * @date 2021/4/12
 */
public class HomePersonalFragment extends BaseFragment<FragmentHomePersonalBinding, HomePersonalViewModel> {

    public static HomePersonalFragment newInstance() {
        return new HomePersonalFragment();
    }

    @Override
    public int initContentView(@NotNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return R.layout.fragment_home_personal;
    }

    @Override
    public int initVariableId() {
        return BR.viewModel;
    }

    @Override
    public void initParam() {

    }

    @Override
    public void initData() {
        ArrayList<String> items = new ArrayList<>();
        items.add("选择器");
        items.add("窗口");
        items.add("Socket,实时接收服务器消息然后显示");
        items.add("表单形式UI");
        items.add("1");
        items.add("1");
        items.add("1");
        items.add("1");
        items.add("1");
        items.add("1");
        items.add("1");
        items.add("1");
        items.add("1");
        items.add("1");
        items.add("1");
        items.add("1");
        items.add("1");
        items.add("1");
        items.add("1");
        items.add("1");
        FunctionAdapter functionAdapter = new FunctionAdapter();
        functionAdapter.setList(items);
        binding.rvMain.setLayoutManager(new GridLayoutManager(getContext(), 3));
        binding.rvMain.setAdapter(functionAdapter);
        binding.rvMain.addItemDecoration(new SpaceItemGridDecoration(DisplayMetricsUtil.dip2px(5), 3));
        functionAdapter.setOnItemClickListener((adapter, view, position) -> {
            switch (position) {
                case 0:
                    // 选择器
                    Intent intentSelector = new Intent();
                    intentSelector.setClass(getActivity(), SelectorActivity.class);
                    startActivity(intentSelector);
                    break;
                case 1:
                    // 窗口
                    viewModel.accept(DialogActivity.startActivity((RxAppCompatActivity) getActivity()));
                    break;
                case 2:
                    // AIS+Socket
                    Intent intentAisSocket = new Intent();
                    intentAisSocket.setClass(getActivity(), AisSocketActivity.class);
                    startActivity(intentAisSocket);
                    break;
                case 3:
                    // from
                    Intent intentFrom = new Intent();
                    intentFrom.setClass(getActivity(), FromActivity.class);
                    startActivity(intentFrom);
                    break;
                default:
                    break;
            }
        });
        binding.toolbar.setTitle("个人中心");
    }

    @Override
    public void initViewObservable() {
        binding.appbar.addOnOffsetChangedListener(new AppBarLayout.OnOffsetChangedListener() {

            boolean misAppbarExpand = true;

            @Override
            public void onOffsetChanged(AppBarLayout appBarLayout, int verticalOffset) {
                int scrollRange = appBarLayout.getTotalScrollRange();
                float fraction = 1f * (scrollRange + verticalOffset) / scrollRange;
                if (fraction < 0.1 && misAppbarExpand) {
                    misAppbarExpand = false;
                    binding.fab.animate().scaleX(0).scaleY(0);
                }
                if (fraction > 0.8 && !misAppbarExpand) {
                    misAppbarExpand = true;
                    binding.fab.animate().scaleX(1).scaleY(1);
                }
            }
        });
    }
}
